import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param nums1 int整型ArrayList
     * @param nums2 int整型ArrayList
     * @return int整型ArrayList
     */
    public ArrayList<Integer> intersection1 (ArrayList<Integer> nums1,
                                            ArrayList<Integer> nums2) {
        // write code here
        ArrayList<Integer> ret = new ArrayList<>();
        int n1 = nums1.size();
        for (int i = 0; i < n1; i++) {
            int n2 = nums2.size();
            boolean[] visited2 = new boolean[n2];
            for (int j = 0; j < nums2.size(); j++) {
                if(visited2[j] == true) {
                    j++;
                    continue;
                }
                if (nums1.get(i).equals(nums2.get(j))) {
                    ret.add(nums1.get(i));
                    visited2[j] = true;
                    break;
                }
            }
        }
        return ret;
    }
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        ArrayList<Integer> ret = new ArrayList<>();
        Set<Integer> set1 = new HashSet<>(nums1);
        Set<Integer> set2 = new HashSet<>(nums2);
        Iterator<Integer> it1 = set1.iterator();
        while(it1.hasNext()) {
            Iterator<Integer> it2 = set2.iterator();
            while(it2.hasNext()) {
                if(it1.next().equals(it2.next())) {
                    ret.add(it1.next());
                }
            }
        }
        return ret;
    }
}